package com.ruoyi.duty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.duty.domain.DutyGzrz;
import com.ruoyi.duty.domain.bo.DutyGzrzBo;
import com.ruoyi.duty.domain.bo.DutyRzFullBo;
import com.ruoyi.duty.domain.vo.DutyGzrzVo;
import com.ruoyi.duty.domain.vo.DutyRzFullVo;
import com.ruoyi.duty.mapper.DutyGzrzMapper;
import com.ruoyi.duty.service.IDutyGzrzService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工作日志Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class DutyGzrzServiceImpl implements IDutyGzrzService {

    private final DutyGzrzMapper baseMapper;

    /**
     * 查询工作日志
     */
    @Override
    public DutyGzrzVo queryById(Long gzrzId) {
        return baseMapper.selectVoById(gzrzId);
    }

    /**
     * 查询工作日志列表
     */
    @Override
    public TableDataInfo<DutyGzrzVo> queryPageList(DutyGzrzBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DutyGzrz> lqw = buildQueryWrapper(bo);
        Page<DutyGzrzVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工作日志列表
     */
    @Override
    public List<DutyGzrzVo> queryList(DutyGzrzBo bo) {
        LambdaQueryWrapper<DutyGzrz> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DutyGzrz> buildQueryWrapper(DutyGzrzBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DutyGzrz> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMemberId() != null, DutyGzrz::getMemberId, bo.getMemberId());
        lqw.eq(bo.getDate() != null, DutyGzrz::getDate, bo.getDate());
        lqw.like(StringUtils.isNotBlank(bo.getContent()), DutyGzrz::getContent, bo.getContent());
        lqw.ge(bo.getStart() != null, DutyGzrz::getDate, bo.getStart());
        lqw.le(bo.getEnd() != null, DutyGzrz::getDate, bo.getEnd());
        return lqw;
    }

    /**
     * 新增工作日志
     */
    @Override
    public Boolean insertByBo(DutyGzrzBo bo) {
        DutyGzrz add = BeanUtil.toBean(bo, DutyGzrz.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGzrzId(add.getGzrzId());
        }
        return flag;
    }

    /**
     * 修改工作日志
     */
    @Override
    public Boolean updateByBo(DutyGzrzBo bo) {
        DutyGzrz update = BeanUtil.toBean(bo, DutyGzrz.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DutyGzrz entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工作日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<DutyRzFullVo> queryRzFull(DutyRzFullBo query) {
        return baseMapper.queryRzFull(query);
    }
}
